package com.cms.work.core.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ServletContextAware;

import com.github.pagehelper.PageInfo;

@Controller
public class BaseController<T> implements ServletContextAware {
    
    public static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    
    public static final String PAGE_NUM = "1";
    public static final String PAGE_SIZE = "10";
    public static final String ORDERBY_NAME = "create_date desc";
    private Map<String, Object> params = new HashMap<String, Object>();
    
    public Map<String, Object> getParams(HttpServletRequest request, Map<String, Object> mapping) {
    	setPage(request);
//    	setOrderBy(request, mapping);
		return params;
	}

	/**
     * <p>Description: 获取排序字段</p>
     * @param orderBy
     * @return
     * @date 2016年11月24日上午12:17:02
     */
    public String getSortColnum(String[] columns, String orderBy){
        String column = orderBy.split(" ")[0];
        for (int i = 0; i < columns.length; i++) {
            if(column.equals(columns[i])){
                return orderBy;
            }
        }
        return ORDERBY_NAME;
    }
    
    @Override
    public void setServletContext(ServletContext servletContext) {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * <p>Description: 获取PageInfo对象</p>
     * @param request
     * @param t
     * @return
     * @throws Exception
     * @date 2016年8月23日下午11:56:41
     */
    public PageInfo<T> getPageInfo(HttpServletRequest request, T t, Map<String, Object> orderMapping) throws Exception{
        PageInfo<T> pageInfo = new PageInfo<T>();
        
        //计算物理分页参数
        Integer pageNum = (request.getParameter("startIndex")==null?1:Integer.valueOf(request.getParameter("startIndex")));
        Integer pageSize = (request.getParameter("pageSize")==null?10:Integer.valueOf(request.getParameter("pageSize")));
        pageNum = pageNum/pageSize+1;
        
        //封装排序参数
        String orderColumn = request.getParameter("orderColumn");
        String orderDir = request.getParameter("orderDir");
        if(null != orderColumn && !"".equals(orderColumn)){
        	orderColumn = (String) orderMapping.get(orderColumn);
        	pageInfo.setOrderBy(orderColumn+" "+orderDir);
        }
        
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        List<T> list = new ArrayList<T>();
        list.add(t);
        pageInfo.setList(list);
        return pageInfo;
    }
    
    public void setPage(HttpServletRequest request){
    	 //计算物理分页参数
        Integer pageNum = (request.getParameter("startIndex")==null?1:Integer.valueOf(request.getParameter("startIndex")));
        Integer pageSize = (request.getParameter("pageSize")==null?10:Integer.valueOf(request.getParameter("pageSize")));
        pageNum = pageNum/pageSize+1;
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
    }
    
    public void setOrderBy(HttpServletRequest request, T t){
    	Map<String, String> orderMapping = new HashMap<String, String>();
    	Class<? extends Object> clazz = t.getClass();
    	Field[] declaredFields = clazz.getDeclaredFields();
    	String orderColumn = request.getParameter("orderColumn");
    	for (int i = 0; i < declaredFields.length; i++) {
    		Field field = declaredFields[i];
    		field.setAccessible(true);
    		
		}
    	
    	//封装排序参数
        String orderDir = request.getParameter("orderDir");
        if(null != orderColumn && !"".equals(orderColumn)){
        	orderColumn = (String) orderMapping.get(orderColumn);
        	params.put("orderColumn", orderColumn);
        	params.put("orderDir", orderDir);
        }
    }
    
    public void getResultDate(HttpServletRequest request, PageInfo<T> pageInfo) throws Exception{
//        Map<String, Object> result = new HashMap<String, Object>();
        params.put("pageData", pageInfo.getList());
        params.put("total", pageInfo.getTotal());
        params.put("draw", request.getParameter("draw"));
    }
    
}
